﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DingTalkAgent.Web.Controllers
{
    [Common.Filter.AuthorizeFilter]
    public class AdminInfoController : Common.adminBaseController
    {
        private DingTalkAgent.SqlData.Admin_Info m_data = new SqlData.Admin_Info();
        [Common.Filter.ViewPage]
        //获取列表信息
        public ActionResult List()
        {
            try
            {
                int records = 0;
                if (Request.IsAjaxRequest())
                {
                    int rows = Convert.ToInt32(Request["rows"] == null ? "10" : Request["rows"].ToString());
                    int page = Convert.ToInt32(Request["page"] == null ? "1" : Request["page"].ToString());
                    var keyword = Request["keyword"] == null ? "" : Request["keyword"].ToString();

                    var list = m_data.GetPageList(page, rows, out records, keyword);
                    var pagelist = new Models.Admin_Info_Page()
                    {
                        records = records,
                        page = page,
                        pagesize = rows,
                        List = list,
                    };
                    return Json(pagelist);
                }

                return View();
            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/AdminInfo/List", ex.Message, _view_Admin_Info.Name, "");
                return Content(ConfigHelper.getErrorMsg);
            }
        }
        //添加信息
        public ActionResult Add()
        {
            SqlData.Roles dh = new SqlData.Roles();
            var l = Models.buildSelect.RoleList(dh.IEnumGetList(""), "0");
            return View(new Model.Admin_Info() { Rolelist = l.ToList() });
        }
        [HttpPost]
        public ActionResult Add(DingTalkAgent.Model.Admin_Info model)
        {
            try
            {
                model.status = 1;
                model.LoginTime = DateTime.Now;
                if (m_data.ExistsAdmin(model.account))
                {
                    return Content("账号已经被注册,请换一个账号!");
                }
                if (m_data.Add(model) > 0)
                {
                    //操作记录
                    Models.Operation.Add(_view_Admin_Info.UserID.ToString(), "添加Admin_Info", "成功添加Admin_Info:", _view_Admin_Info.Name, "");
                    return Content("true");
                }
                else
                {
                    return Content("保存信息失败!");
                }

            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/AdminInfo/Add", ex.Message, _view_Admin_Info.Name, "");
                return Content(ConfigHelper.getErrorMsg);
            }

        }
        //修改信息
        public ActionResult Edit(int id)
        {
            try
            {
                var m = m_data.GetModel(id);
                if (m == null)
                {

                    ViewBag.Msg = "管理员已经被删除！";
                    return Content(ViewBag.Msg);
                }
                SqlData.Roles dh = new SqlData.Roles();
                var l = Models.buildSelect.RoleList(dh.IEnumGetList(""), "0");
                m.Rolelist = l.ToList();
                return View(m);
            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/AdminInfo/Edit", ex.Message, _view_Admin_Info.Name);
                ViewBag.Msg = "查找的信息不存在!";
                return View("Error");
            }
        }

        [HttpPost]
        public ActionResult Edit(DingTalkAgent.Model.Admin_Info model)
        {
            try
            {
                model.status = 1;
                model.LoginTime = DateTime.Now;

                    if (m_data.ExistsAdmin(model.account))
                    {
                        return Content("账号已经被注册,请换一个账号!");
                    }
                
                if (m_data.Update(model))
                {
                    //操作记录
                    Models.Operation.Add(_view_Admin_Info.UserID.ToString(), "修改Admin_Info", "成功修改Admin_Info:", _view_Admin_Info.Name, "");
                    return Content("true");
                }
                else
                {
                    return Content("保存信息失败！");
                }
            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/AdminInfo/Edit", ex.Message, _view_Admin_Info.Name);
                return Content(ConfigHelper.getErrorMsg);
            }
        }
        //删除信息
        [HttpPost]
        public ActionResult Delete(int id)
        {
            try
            {
                if (m_data.Delete(id))
                {
                    //操作记录
                    Models.Operation.Add(_view_Admin_Info.UserID.ToString(), "删除Admin_Info", "成功删除Admin_Info:", _view_Admin_Info.Name, "");
                    return Content("true");
                }
                else
                {
                    return Content("删除信息失败!");
                }

            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/AdminInfo/Delete", ex.Message, _view_Admin_Info.Name);
                return Content(ConfigHelper.getErrorMsg);
            }
        }

        //删除多个信息
        [HttpPost]
        public ActionResult Deletes(string ids)
        {
            try
            {
                //
                ids = Discuz.Common.Utils.ChkSQL(ids).TrimEnd(',');
                if (m_data.Deletes(ids))
                {
                    //操作记录
                    Models.Operation.Add(_view_Admin_Info.UserID.ToString(), "批量删除Admin_Info", "批量成功删除Admin_Info:", _view_Admin_Info.Name, "");
                    return Content("true");
                }
                else
                {
                    return Content("删除信息失败!");
                }

            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/AdminInfo/Deletes", ex.Message, _view_Admin_Info.Name);
                return Content(ConfigHelper.getErrorMsg);
            }
        }

        [Common.myCommon]
        //更改状态
        public ActionResult Check(int ID, int status)
        {
            try
            {
                if (m_data.UpdateStatus(ID, status))
                {
                    //操作记录
                    Models.Operation.Add(_view_Admin_Info.UserID.ToString(), "状态变更Admin_Info", "成功状态变更Admin_Info:", _view_Admin_Info.Name, "");
                    return Content("1");
                }
                else
                {
                    return Content("0");
                }
            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/AdminInfo/Check", ex.Message, _view_Admin_Info.Name, "");
                return Content("-1");
            }
        }
    }

}